#include "softregist.h"
#include "ui_softregist.h"
#include"QRCode/qrencode.h"
#include "QPainter"
#include"others/wmic.h"
#include<qdebug.h>
#include <Windows.h>
#include<qsettings.h>
#include"dialog/myhelper.h"

SoftRegist::SoftRegist(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::SoftRegist)
{
    ui->setupUi(this);
    setBg();
//    this->setWindowFlags(Qt::FramelessWindowHint);
    if(Constant::ShowFullScreen)
        this->showFullScreen();
    welcomePage=new WelcomePage();
    welcomePage->hide();

    flag=false;
    QCoreApplication::setOrganizationName(QString("QXZD"));
    QCoreApplication::setApplicationName(QString("Qt"));
    QSettings settings(QSettings::NativeFormat, QSettings::UserScope, QCoreApplication::organizationName(), QCoreApplication::applicationName());
//    settings.setValue("Name", "1");
    WMIC wmic;
    id=wmic.getCpuId();
    QString id_r=wmic.MachineCodetoIDNumber(settings.value("Name").toString());
    QString mc=wmic.IDNumbertoMachineCode(id);

    if(id==id_r){
        ui->pushButton->setVisible(false);
        ui->pb_Enter->setVisible(true);
        ui->pushButton_2->setVisible(false);
        ui->soft_regist_lineEdit->setVisible(false);
        ui->label_2->setVisible(false);
        ui->softRegist_label_3->setVisible(false);
        ui->softRegist_label->setVisible(false);
        welcomePage->show();
        this->close();
    }else{
        ui->pushButton->setVisible(true);
        ui->pb_Enter->setVisible(false);
        ui->pushButton_2->setVisible(true);
        ui->softRegist_label->setVisible(true);
        ui->soft_regist_lineEdit->setVisible(true);
        ui->label_2->setVisible(true);
        ui->softRegist_label_3->setVisible(true);
        ui->label_2->setPixmap(createQRCode(id));

    }
}

SoftRegist::~SoftRegist()
{
    delete welcomePage;
    delete ui;
}
void SoftRegist::paintEvent(QPaintEvent*){



}
QPixmap SoftRegist::createQRCode(const QString &text)
{
    int margin = 2;
    if (text.length() == 0)
    {
        return QPixmap();
    }
    QRcode *qrcode = QRcode_encodeString(text.toLocal8Bit(), 2, QR_ECLEVEL_L, QR_MODE_8, 0);
    if (qrcode == NULL) {
        return QPixmap();
    }
    unsigned char *p, *q;
    p = NULL;
    q = NULL;
    int x, y, bit;
    int realwidth;

    realwidth = qrcode->width;
    QImage image = QImage(realwidth, realwidth, QImage::Format_Indexed8);
    QRgb value;
    value = qRgb(255,255,255);
    image.setColor(0, value);
    value = qRgb(0,0,0);
    image.setColor(1, value);
    image.setColor(2, value);
    image.fill(0);
    p = qrcode->data;
    for(y=0; y<qrcode->width; y++) {
        bit = 7;
        q += margin / 8;
        bit = 7 - (margin % 8);
        for(x=0; x<qrcode->width; x++) {
            if ((*p & 1) << bit)
                image.setPixel(x, y, 1);
            else
                image.setPixel(x, y, 0);
            bit--;
            if(bit < 0)
            {
                q++;
                bit = 7;
            }
            p++;
        }
    }
    return QPixmap::fromImage(image.scaledToWidth(150));
}


void SoftRegist::on_pushButton_clicked()
{
    QCoreApplication::setOrganizationName(QString("QXZD"));
    QCoreApplication::setApplicationName(QString("Qt"));
    QSettings settings(QSettings::NativeFormat, QSettings::UserScope, QCoreApplication::organizationName(), QCoreApplication::applicationName());
    QString MachineCode=ui->soft_regist_lineEdit->text();
    WMIC wmic;
    QString res=wmic.getCpuId();
    QString ID=wmic.MachineCodetoIDNumber(MachineCode);
    if(ID==res){
        myHelper::ShowMessageBoxInfo("恭喜您，注册成功！");
        settings.setValue("Name", MachineCode);
//        Login *logw=new Login;
//        logw->show();
        welcomePage->show();
        this->close();

    }else{
        myHelper::ShowMessageBoxError("注册失败，请联系技术支持！");
    }

}

void SoftRegist::on_pushButton_2_clicked()
{
    this->close();
}

void SoftRegist::on_pushButton_3_clicked()
{

}

void SoftRegist::on_pb_Enter_clicked()
{
//    Login *logw=new Login;
//    logw->show();
    welcomePage->show();
    this->close();
}
/**
 * 功能：设置背景图片
 * @brief WelcomePage::setBg
 */
void SoftRegist::setBg()
{
    QPalette pal = this->palette();
    pal.setBrush(QPalette::Window,QBrush(QPixmap(":/images/welcomePage/bg_welcome.png")));
    setPalette(pal);
}
